package com.huali.dao;

import com.alibaba.druid.pool.DruidDataSourceFactory;


import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class BaseDao {
//    数据源对象
    private static DataSource dataSource = null;

    //静态代码块
    static {
        Properties properties = new Properties();
        try {
//            读取配置文件
            properties.load(BaseDao.class.getClassLoader().getResourceAsStream("druid.properties"));
//            创建数据
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static Connection getConnection() throws SQLException {
        Connection connection = dataSource.getConnection();
        return connection;
    }
    public static int executeUpdate(String sql,Object... data){
        //数据库连接对象
        Connection conn = null;
        //sql执行对象
        PreparedStatement ps = null;
//        返回值
        int res = 0;
        try {
//            调用方法获取数据库连接对象
            conn = getConnection();
//            获取sql执行对象
            ps = conn.prepareStatement(sql);
//            进行属性赋值
            for (int i = 0; i < data.length; i++) {
                ps.setObject(i+1,data[i]);
            }
//            执行sql获取响应
            res = ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return  res;
    }
    public static List<Map<String,Object>> executeQuery(String sql, Object... data){
//        数据库连接对象
        Connection conn = null;

        PreparedStatement ps = null;

        ResultSet rs = null;

        List<Map<String, Object>> list = new ArrayList<>();

        try {
            conn = getConnection();

            ps = conn.prepareStatement(sql);

            for (int i = 0; i < data.length; i++) {
                ps.setObject(i+1,data[i]);
            }
            rs = ps.executeQuery();

            int count = rs.getMetaData().getColumnCount();
            
            while (rs.next()){
//                创建存放一条数据的对象
                Map<String, Object> map = new HashMap<String,Object>();

                for (int i = 1; i < count; i++) {
//                    获取字段名
                    String name = rs.getMetaData().getColumnLabel(i);
//                    获取值
                    Object values = rs.getObject(i);
//                    把名字和值存到map里面
                    map.put(name,values);
                }
                list.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            close(conn,ps,rs);
        }
        return list;
    }
    private static void close(Connection conn, PreparedStatement ps , ResultSet rs){
        if (conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
